#!/bin/sh

TEST_DIR=''
cleanup() {
  unset -f nvm_ls_current nvm_has nvm_download sh
  case "${TEST_DIR}" in
    /*) rm -rf "${TEST_DIR}" ;;
  esac
}

die() { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

TEST_DIR="$(mktemp -d)" || die 'unable to create a temp dir'
[ -n "${TEST_DIR}" ] || die 'unable to create a temp dir'
NVM_DIR="${TEST_DIR}/nvm"
mkdir -p "${NVM_DIR}" || die 'unable to create NVM_DIR'

unset clean npm_install

nvm_ls_current() { nvm_echo "${CURRENT}"; }
nvm_has() {
  nvm_echo "$*" >> "${TEST_DIR}/has"
  return "${HAS_STATUS}"
}
nvm_download() {
  nvm_echo "$*" > "${TEST_DIR}/download"
  nvm_err 'fake download stderr'
  nvm_echo 'fake install script'
}
sh() {
  {
    nvm_echo "args: >$*<"
    nvm_echo "clean: >${clean-unset}<"
    nvm_echo "npm_install: >${npm_install-unset}<"
    nvm_echo "stdin: >$(cat)<"
  } > "${TEST_DIR}/sh"
  nvm_err 'fake sh stderr'
  nvm_echo 'fake sh output'
  return "${SH_STATUS}"
}

run() {
  rm -f "${TEST_DIR}/has" "${TEST_DIR}/download" "${TEST_DIR}/sh" "${TEST_DIR}/stdout" "${TEST_DIR}/stderr"
  nvm_install_npm_if_needed > "${TEST_DIR}/stdout" 2> "${TEST_DIR}/stderr" && STATUS=0 || STATUS=$?
  STDOUT="$(cat "${TEST_DIR}/stdout")"
  STDERR="$(cat "${TEST_DIR}/stderr")"
  HAS="$(cat "${TEST_DIR}/has" 2>/dev/null)"
}

assert_eq() {
  [ "_${2}" = "_${3}" ] || die "${1}: got >${2}<, expected >${3}<"
}

assert_not_run() {
  [ ! -e "${TEST_DIR}/${2}" ] || die "${1}: ${2} should not have been called"
}

DOWNLOAD_ARGS='-L https://npmjs.org/install.sh -o -'
INSTALL_STDERR='fake download stderr
fake sh stderr'
SH_STATUS=0

# npm already available: nothing happens, whatever the version
HAS_STATUS=0
for CURRENT in v0.1.0 v0.2.1 v20.0.0 none system iojs-v1.0.0 ''; do
  run
  assert_eq "npm present, current >${CURRENT}<: status" "${STATUS}" 0
  assert_eq "npm present, current >${CURRENT}<: stdout" "${STDOUT}" ''
  assert_eq "npm present, current >${CURRENT}<: stderr" "${STDERR}" ''
  assert_eq "npm present, current >${CURRENT}<: nvm_has arg" "${HAS}" 'npm'
  assert_not_run "npm present, current >${CURRENT}<" download
  assert_not_run "npm present, current >${CURRENT}<" sh
done

HAS_STATUS=1

# node older than v0.2.3: an error, and no download
for CURRENT in v0.1.104 0.1.104 v0.2.0 v0.2.2; do
  run
  assert_eq "current ${CURRENT}: stdout" "${STDOUT}" 'Installing npm...'
  assert_eq "current ${CURRENT}: stderr" "${STDERR}" 'npm requires node v0.2.3 or higher'
  assert_eq "current ${CURRENT}: nvm_has arg" "${HAS}" 'npm'
  assert_not_run "current ${CURRENT}" download
  assert_not_run "current ${CURRENT}" sh
done

# v0.2.3 and up: the install script is piped to sh, pinned to npm 0.2.19
for CURRENT in v0.2.3 0.2.3 v0.2.9; do
  run
  assert_eq "current ${CURRENT}: status" "${STATUS}" 0
  assert_eq "current ${CURRENT}: stdout" "${STDOUT}" "Installing npm...
fake sh output"
  assert_eq "current ${CURRENT}: stderr" "${STDERR}" "${INSTALL_STDERR}"
  assert_eq "current ${CURRENT}: nvm_download args" "$(cat "${TEST_DIR}/download")" "${DOWNLOAD_ARGS}"
  assert_eq "current ${CURRENT}: sh" "$(cat "${TEST_DIR}/sh")" "args: ><
clean: >yes<
npm_install: >0.2.19<
stdin: >fake install script<"
done

# the install script's exit status is returned
SH_STATUS=7
VERSION='sentinel'
for CURRENT in v0.2.3; do
  run
  assert_eq "current >${CURRENT}<, failing install script: status" "${STATUS}" 7
done

# no variables leak into the caller
assert_eq 'VERSION after the call' "${VERSION}" 'sentinel'
assert_eq 'clean after the call' "${clean-unset}" 'unset'
assert_eq 'npm_install after the call' "${npm_install-unset}" 'unset'

cleanup
